
var app = getApp();
var getAppGlobalData = require('../../../templates/copyright/copyright.js');

Page({
  imagePath: '',
  data: {
    template: {},
    globalData: {},
    shareData: {},
    qrImg: ''
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    console.log("onLoad ==>", this);
    var that = this; 
    getAppGlobalData.getAppGlobalData(that);
    that.setData({
      globalData : app.globalData
    })
    wx.hideShareMenu(); 
    that.getShareCard();
  },
  onReady: function () {
    // 页面渲染完成
    var that = this;
  },
  onShow: function () {
    // 页面显示
    var that = this;
  },
  onHide: function () {
    // 页面隐藏 
  },
  onUnload: function () {
    // 页面关闭 
  },
  onPullDownRefresh: function () {
    // 监听用户下拉动作
  },
  onReachBottom: function () {
    // 页面上拉触底
  },
  onShareAppMessage: function (res) {
    // 用户点击右上角分_享 
  }, 
  getShareCard: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/card',
      'cachetime': '30',
      'method': 'POST',
      'data': {
        to_uid: that.data.globalData.to_uid,
        from_id: that.data.globalData.from_id
      },
      success: function (res) {
        console.log("entry/wxapp/card ==>", res)
        if (!res.data.errno) {
          var tmpImgs = [];
          tmpImgs.push(res.data.data.info.avatar)
          tmpImgs.push(that.data.globalData.company.logo)
          that.setData({
            shareData: res.data.data
          })

          var tmpUserLogo = tmpImgs;
          for(let i in tmpUserLogo){

            app.util.request({
              'url': 'entry/wxapp/image',
              'cachetime': '30',
              'method': 'POST',
              'responseType': 'arraybuffer',
              'data': {
                imgUrl: tmpUserLogo[i],
                to_uid: that.data.shareData.to_uid
              },
              success: function (res) {
                console.log("entry/wxapp/image ==>", res)
                if (!res.data.errno) {
                  tmpUserLogo[i] = res.data.data.image;
                  tmpUserLogo[i] = tmpImgs[i]
                }
              },
              fail: function (res) {
                console.log("fail ==> ",res)
              }
            })

            that.setData({
              tmpImgs : tmpUserLogo
            })
          }

          
          that.toGetQR();

        }
      },
      fail: function (res) {
        console.log("fail ==> ",res)
      }
    })
  }, 
  toGetQR: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/qr',
      'cachetime': '30',
      'method': 'POST',
      'data': {
        to_uid: app.globalData.to_uid
      },
      success: function (res) {
        console.log("获取二维码 entry/wxapp/qr ==>", res)
        if (!res.data.errno) {
          console.log(res.data.data.image,"res.data.data.image  获取二维码")
          that.setData({
            qrImg: res.data.data.image
          })

          let tmpData = {
            width: '670rpx',
            height: '1070rpx',
            background: '#eee',
            views: [
              {
                type: 'rect',
                css: {
                  top: '0rpx',
                  left: '0rpx',
                  color: 'white',
                  borderRadius: '15rpx',
                  borderWidth: '0rpx',
                  width: '668rpx',
                  height: '1068rpx',
                },
              },
              {
                type: 'image',
                url: that.data.tmpImgs[0],
                css: {
                  top: '0rpx',
                  left: '0rpx',
                  width: '670rpx',
                  height: '670rpx',
                  rotate: 0,
                  borderRadius: 0,
                },
              },
              {
                type: 'image',
                url: '/longbing_card/resource/images/circle.png',
                css: {
                  top: '622rpx',
                  left: '211rpx',
                  width: '257rpx',
                  height: '57rpx',
                  rotate: 0,
                  borderRadius: 0,
                },
              },
              {
                type: 'image',
                url: that.data.tmpImgs[1],
                css: {
                  top: '630rpx',
                  left: '290rpx',
                  width: '100rpx',
                  height: '100rpx',
                  rotate: 0,
                  borderRadius: '100rpx',
                },
              },
              {
                type: 'image',
                url: that.data.qrImg,
                css: {
                  top: '880rpx',
                  left: '470rpx',
                  width: '170rpx',
                  height: '170rpx',
                  rotate: 0,
                  borderRadius: 0,
                },
              },
              {
                type: 'text',
                text: that.data.shareData.info.name,
                css: {
                  fontSize: '34rpx',
                  top: '755rpx',
                  left: '340rpx',
                  color: '#313131',
                  textDecoration: 'none',
                  align: 'center',
                  width: '670rpx',
                },
              },
              {
                type: 'text',
                text: that.data.shareData.info.job_name,
                css: {
                  fontSize: '24rpx',
                  top: '800rpx',
                  left: '340rpx',
                  color: '#9a9a9a',
                  textDecoration: 'none',
                  align: 'center',
                  width: '670rpx',
                },
              },
              {
                type: 'text',
                text: that.data.globalData.company.name,
                css: {
                  fontSize: '24rpx',
                  top: '840rpx',
                  left: '340rpx',
                  color: '#9a9a9a',
                  textDecoration: 'none',
                  align: 'center',
                  width: '670rpx',
                },
              },
              {
                type: 'text',
                text: '手机',
                css: {
                  fontSize: '28rpx',
                  top: '900rpx',
                  left: '30rpx',
                  color: '#838591',
                  textDecoration: 'none',
                  align: 'left',
                  width: '100rpx',
                },
              },
              {
                type: 'text',
                text: that.data.shareData.info.phone,
                css: {
                  fontSize: '28rpx',
                  top: '900rpx',
                  left: '100rpx',
                  color: '#343541',
                  textDecoration: 'none',
                  align: 'left',
                  width: '380rpx',
                },
              },
              {
                type: 'text',
                text: '微信',
                css: {
                  fontSize: '28rpx',
                  top: '940rpx',
                  left: '30rpx',
                  color: '#838591',
                  textDecoration: 'none',
                  align: 'left',
                  width: '100rpx',
                },
              },
              {
                type: 'text',
                text: that.data.shareData.info.wechat,
                css: {
                  fontSize: '28rpx',
                  top: '940rpx',
                  left: '100rpx',
                  color: '#343541',
                  textDecoration: 'none',
                  align: 'left',
                  width: '380rpx',
                },
              },
              {
                type: 'text',
                text: '地址',
                css: {
                  fontSize: '28rpx',
                  top: '980rpx',
                  left: '30rpx',
                  color: '#838591',
                  textDecoration: 'none',
                  align: 'left',
                  width: '100rpx',
                },
              },
              {
                type: 'text',
                text: that.data.globalData.company.addr,
                css: {
                  fontSize: '28rpx',
                  top: '980rpx',
                  left: '100rpx',
                  color: '#343541',
                  textDecoration: 'none',
                  align: 'left',
                  width: '380rpx',
                },
              },
            ]
          }
          that.setData({
            template: tmpData
          })
        }
      },
      fail: function (res) {
        console.log("fail ==> ",res)
      }
    })
  },
  onImgOK: function (e) {
    var that = this;
    that.setData({
      imagePath: e.detail.path
    })
    console.log(e);
  },
  toShareRecord: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/record',
      'cachetime': '30',
      'showLoading': false,
      'method': 'POST',
      'data': { 
        to_uid: app.globalData.to_uid
      },
      success: function (res) {
        // console.log("entry/wxapp/record ==>", res)
        if (!res.data.errno) {
        }
      },
      fail: function (res) {
        console.log("fail ==> ",res)
      }
    })
  }, 
  saveImage() {
    var that = this;
    console.log("点击保存海报")
    wx.saveImageToPhotosAlbum({
      filePath: that.data.imagePath,
      success: function (res) {
        console.log("保存名片成功 ==>", res)
        that.toShareRecord();
      },
      fail: function (res) {
        console.log("fail ==> ",res)
      }
    });
  }
})